// pages/courttracks/detaile/index.js
const app = getApp()
import api from '../../../api/main.js'
import tools from '../../../utils/tools.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    data: {},
    courtName:'',
    title:'',
    status:0,
    images:[],
    content:'',
    showNumber:'',
    publishDate:'',
    insertTime:'',
    userType:0,
    width: app.globalData.screenWidth
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    if (options.id) {
      var publishDate = ''
      const insertTime = parseInt(options.insertTime)
      if(insertTime){
        publishDate = tools.stringToDate(insertTime, 'Y-M-D h:m:s') + ' ' + options.publisher +' - 发布'
      }else{
        publishDate = options.publishDate
      }
      const array = options.images.split(',')
      var images = []
      for (var i = 0; i < array.length; i++) {
        images.push(app.resourcesFullUrl(array[i]))
      }
      console.log('11')
      that.setData({
        id: options.id,
        courtName: options.courtName,
        title: options.title,
        status: options.status,
        images: images,
        content: options.content,
        showNumber: parseInt(options.showNumber) || 0,
        publishDate: publishDate,
        userType:app.globalData.userType
      })
    }
    that.setCourtTracksRead()
  },
  /**
   * 阅读标记
   */
  setCourtTracksRead(){
    var that = this
    if(that.data.status == 1){
      api.track.setCourtTrackRead(that.data.id).then(res => {
        if (res.code == app.globalData.successCode && res.success) {
          //更新上页面数据
          const pages = getCurrentPages();
          const prevPage = pages[pages.length - 2]; //上一个页面
          prevPage.loadData()
          that.setData({
            showNumber: that.data.showNumber + 1
          })
        }
      })
    }
  },
  previewImage: function (e) {
    var that = this
    const index = parseInt(e.currentTarget.dataset.index)
    wx.previewImage({
      //当前显示下表
      current: that.data.images[index],
      //数据源
      urls: that.data.images
    })
  },
  /**
   * 审核/驳回动态
   * index 1 审核 -1 驳回
   */
  onModify(e){
    var that = this
    const index = parseInt(e.currentTarget.dataset.index)
    var msg = '您确定要审核该动态吗？'
    if(index == -1){
      msg = '您确定要驳回该动态吗？'
    }
    wx.showModal({
      title: '温馨提示',
      content: msg,
      success(res) {
        if (res.confirm) {
          that.modifyStatus(index)
        }
      }
    })
  },
  /**
   * 请求接口修改动态状态
   */
  modifyStatus(status){
    var that = this
    wx.showLoading({
      title: '正在更改...',
    })
    api.track.modifyStatus(that.data.id , status).then(res => {
      wx.hideLoading()
      if (res.code == app.globalData.successCode && res.success) {
        //更新上页面数据
        const pages = getCurrentPages();
        const prevPage = pages[pages.length - 2]; //上一个页面
        prevPage.loadData()
        that.setData({
          status: status
        })
      }else {
        wx.showToast({
          title: res.msg,
          icon:'none'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})